package plugin;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.JLabel;

public class VerticalLabel extends JLabel {
	private static final long serialVersionUID = 1L;
	public static final int ROTATE = 11;
	public static final int BELOW = 22;
	private int style;

	protected VerticalLabel(String aText, int style) {
		super(aText,JLabel.CENTER);
		setStyle(style);
		setFont(new Font("Dialog",Font.BOLD,10));
	}

	protected void setStyle(int style) {
		this.style = (style != ROTATE && style != BELOW ? ROTATE : style);
	}

	@Override
	public void paintComponent(Graphics g) {
		if (style == ROTATE) {
			AffineTransform at = new AffineTransform();
			Graphics2D g2D = (Graphics2D) g;
			g2D.setColor(getBackground());
			g2D.fillRect(0, 0, getSize().width, getSize().height);
			at.setToTranslation(getSize().getWidth(), getSize().getHeight());
			g2D.setFont(new Font("Dialog", Font.BOLD, 10));
			g2D.transform(at);
			at.setToRotation(Math.PI + (Math.PI / 2.0));
			g2D.transform(at);
			FontMetrics fontMetrics = g2D.getFontMetrics();
			Rectangle2D rect = fontMetrics.getStringBounds(getText(), g2D);
			g2D.setColor(getForeground());
			g2D.drawString(getText(),
					(getSize().height / 2) - (int) (rect.getCenterX()) + 1, -1);
		} else {			
			convertTextToHTML();
			super.paintComponent(g);
		}
	}
	
	// @Override
	// public void paintComponent(Graphics g) {
	// super.paintComponent(g);
	// Graphics2D g2d = (Graphics2D) g;
	// g2d.rotate(Math.toRadians(270.0));
	// g2d.drawString(getText(), 0, 0);
	// }

	private void convertTextToHTML() {
		if((getText().length()<5) || !getText().substring(0, 6).equals("<html>")) {
			String result = "<html>";
			for (int i = 0; i<getText().length();i++) {
				result+=getText().charAt(i);
				result+="<br>";
			}
			result+="</html>";
			setText(result);			
		}
	}

}
